from resource.script.Char.lib.Char import Char

buffs = [ ]
hot = [ ]


class Player ( Char ) :
	def __init__ ( self , data ) :
		super ( Player , self ).__init__ ( data )

	def _hit_js ( self , val ) :
		self.js -= val

	def _hit_hp ( self , val ) :
		if self.dd > 0 :
			self.dd = self.dd - val
		if self.dd < 0 :
			val = -self.dd
			self.dd = 0
			self.hp -= val
		elif self.dd == 0 :
			self.hp -= val

	def _hit_enemy ( self , enemy ) :
		enemy.hp -= self.ad

	def _add_js ( self , val ) :
		self.js += val

	def _add_hp ( self , val ) :
		self.hp += val

	def _add_dd ( self , val ) :
		self.dd += val

	def _get_item ( self , id , type , num = 1 ) :
		it = { }
		it [ 'id' ] = id
		it [ 'num' ] = num
		if type == 1 :
			if self.item :
				if self._have_item ( id ) == 0 :
					self.item.append ( it )
				else :
					ids = [ ]
					nums = [ ]
					for x in self.item :
						ids.append ( x [ 'id' ] )
						nums.append ( x [ 'num' ] )
					for x in self.item :
						if x [ 'id' ] == id :
							x [ 'num' ] += int ( num )
			else :
				self.item.append ( it )
		elif type == 2 :
			self.item.append ( it )
		print "Git Item " + str ( id ) + " * " + str ( num ) + " | Now Items " + str ( self.item )

	def _have_item ( self , id ) :
		have = False
		if len ( self.item ) :
			ids = [ ]
			nums = [ ]
			for x in self.item :
				ids.append ( x [ 'id' ] )
				nums.append ( x [ 'num' ] )
			if id in ids :
				have = True
			else :
				have = False
			if have :
				for key , i_d in enumerate ( ids ) :
					if i_d == id :
						return nums [ key ]
		return 0

	def _use_item ( self , id , num = 1 ) :
		if self.item :
			if self._have_item ( id ) >= num :
				ids = [ ]
				nums = [ ]
				for x in self.item :
					ids.append ( x [ 'id' ] )
					nums.append ( x [ 'num' ] )
				for x in self.item :
					if x [ 'id' ] == id :
						x [ 'num' ] -= int ( num )
						if x [ 'num' ] <= 0 :
							self.item.remove ( x )
						print "Use Item " + str ( id ) + " * " + str ( num ) + " | Now Items " + str ( self.item )
						return True
			else :
				print "No More Item " + str ( id )
		else :
			print 'No Item ' + str ( id )
			return False

	def _hit_mp ( self , val ) :
		mp = self.mp
		mp -= val
		if mp < 0 :
			return False
		else :
			self.mp = mp
			return True

	def _add_mp ( self , val ) :
		self.mp += val

	def _add_buff ( self , id , time ) :
		have = False
		bf = { }
		bf [ 'id' ] = id
		bf [ 'time' ] = time
		if buffs :
			ids = [ ]
			for x in buffs :
				ids.append ( x [ 'id' ] )
			if id in ids :
				have = True
			else :
				have = False

			if have :
				for x in buffs :
					if x [ 'id' ] == id :
						x [ 'time' ] += int ( time )
			else :
				buffs.append ( bf )
		else :
			buffs.append ( bf )
